﻿namespace PayPal.ButtonManager
{
    using System;
    using System.Collections.Generic;

    using com.paypal.sdk.services;
    using com.paypal.sdk.util;

    public static class BuyNowButton
    {
        public static ButtonManagerResponse Create(string business, string itemName, string amount)
        {
            HtmlButtonVariables buttonVariables = new HtmlButtonVariables()
            {
                Business = business,
                ItemName = itemName,
                Amount = amount,
            };

            return Create(buttonVariables);
        }

        public static ButtonManagerResponse Create(
                            HtmlButtonVariables buttonVariables,
                            ButtonOption[] buttonOptions = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "",
                            string buyNowText = "")
        {
            return Create(HtmlButtonVariablesHelper.GetProperties(buttonVariables), buttonOptions, buttonImage, buttonImageUrl, buttonSubType, buttonCountry, buttonLanguage, buyNowText);
        }

        public static ButtonManagerResponse Create(
                            Dictionary<string, string> vars,
                            ButtonOption[] opts = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "",
                            string buyNowText = "")
        {
            NVPCallerServices caller = new NVPCallerServices();
            caller.APIProfile = Profile.BMProfile;

            NVPCodec encoder = NameValuePairApiHelper.PrepareEncoder(
                                method: "BMCreateButton",
                                buttonCode: "HOSTED",
                                buttonType: "BUYNOW",                                
                                buttonSubType: buttonSubType,
                                buttonImage: buttonImage,
                                buttonImageUrl: buttonImageUrl,
                                buttonCountry: buttonCountry,
                                buttonLanguage: buttonLanguage,
                                buyNowText: buyNowText);
           
            NameValuePairApiHelper.PrepareButtonVars(encoder, vars, opts);
            
            string response = caller.Call(encoder.Encode());

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(response);            

            if (decoder.Get("ACK").Equals("Failure", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ButtonManagerException(decoder.Get("L_ERRORCODE0"), decoder.Get("L_SHORTMESSAGE0"), decoder.Get("L_LONGMESSAGE0"));
            }

            return NameValuePairApiHelper.PrepareResponse(decoder);
        }

        public static ButtonManagerResponse Update(
                string hostedButtonId,
                string business,
                string itemName,
                string amount)
        {
            HtmlButtonVariables buttonVariables = new HtmlButtonVariables()
            {
                Business = business,
                ItemName = itemName,
                Amount = amount,
            };

            return Update(hostedButtonId, HtmlButtonVariablesHelper.GetProperties(buttonVariables));
        }

        public static ButtonManagerResponse Update(
                            string hostedButtonId,
                            HtmlButtonVariables buttonVariables,
                            ButtonOption[] buttonOptions = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "",
                            string buyNowText = "")
        {
            return Update(hostedButtonId, HtmlButtonVariablesHelper.GetProperties(buttonVariables), buttonOptions, buttonImage, buttonImageUrl, buttonSubType, buttonCountry, buttonLanguage, buyNowText);
        }

        public static ButtonManagerResponse Update(
                            string hostedButtonId,
                            Dictionary<string, string> buttonVariables,
                            ButtonOption[] buttonOptions = null,
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "",
                            string buyNowText = "")
        {
            NVPCallerServices caller = new NVPCallerServices();
            caller.APIProfile = Profile.BMProfile;

            NVPCodec encoder = NameValuePairApiHelper.PrepareEncoder(
                    method: "BMUpdateButton",
                    buttonCode: "HOSTED",
                    buttonType: "BUYNOW",
                    hostedButtonId: hostedButtonId,
                    buttonSubType: buttonSubType,
                    buttonImage: buttonImage,
                    buttonImageUrl: buttonImageUrl,
                    buttonCountry: buttonCountry,
                    buttonLanguage: buttonLanguage,
                    buyNowText: buyNowText);

            NameValuePairApiHelper.PrepareButtonVars(encoder, buttonVariables, buttonOptions);
            
            string response = caller.Call(encoder.Encode());

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(response);            

            if (decoder.Get("ACK").Equals("Failure", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ButtonManagerException(decoder.Get("L_ERRORCODE0"), decoder.Get("L_SHORTMESSAGE0"), decoder.Get("L_LONGMESSAGE0"));
            }

            return NameValuePairApiHelper.PrepareResponse(decoder);
        }

        public static void Delete(string hostedButtonId)
        {
            ButtonManagerHelper.DeleteButton(hostedButtonId);
        }
    }
}
